package model;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.swing.JLabel;

public class ApplicationModel {

	
	private HashMap<String,String> dictionary;
	
	private HashMap<String,HashMap<String,String>> traductor;
	
	private WordListModel wordList ;
	
	private PropertyChangeSupport propertyChange ;

	
	
	public ApplicationModel(){
		
		wordList = new WordListModel();
		propertyChange = new PropertyChangeSupport(this);
	    
		
	
		dictionary = new HashMap<String,String>();
		
		traductor = new HashMap<String,HashMap<String,String>>();
//		readDico("/Users/vincentpoulain/Dropbox/NF28/TD2/workspace/nf28td2/res/dico.txt");
//		wordList.addWords(this.getListWord());
	}
	
	public void init(final String path){
		wordList.clear();
		readDico(path);
		wordList.addWords(this.getListWord());
	}
	
	public void changeDef(final String name,final String language){

		propertyChange.firePropertyChange("def",null, dictionary.get(name));
		propertyChange.firePropertyChange("trad",null, traductor.get(name).get(language));
	}
	
	public void readDico(final String path){
		
		 try{
			  FileInputStream fstream = new FileInputStream(path);
			  DataInputStream in = new DataInputStream(fstream);
			  BufferedReader br = new BufferedReader(new InputStreamReader(in));
			  String strLine;
			  String[] temp;
			  String[] temp2;
			  String name;
			  
			  while ((strLine = br.readLine()) != null)   {
			  
					// Dico  
				  
				  
				  temp = strLine.split("=");
				  temp2 = temp[1].split("/");
				  dictionary.put(temp[0], temp2[0]);
				  name = temp[0];
				  
				  // trad
				  
				  temp = temp2[1].split(",");
				  HashMap<String, String> listTrad = new HashMap<String,String>();
				  for(int i = 0;i<temp.length;i++){
					  temp2 = temp[i].split(":");
					  listTrad.put(temp2[0],temp2[1]);
					  
				  }
			  
				  traductor.put(name, listTrad);
			  }
			  in.close();
			}
		 catch (Exception e){//Catch exception if any
			  System.err.println("Error: " + e.getMessage());
		 }
		
	}
	
	public List<String> getListWord(){
		
		List<String> listWord = new ArrayList<String>();
		
		if(dictionary.isEmpty()){
			System.err.println("Error: dictonnary empty, are you readsDico ?");
			return listWord;
		}
		else{
			 listWord.addAll(dictionary.keySet());
			 return listWord;
		}
	}

	public WordListModel getWordList() {
		return wordList;
	}


	public void setWordList(WordListModel wordList) {
		this.wordList = wordList;
	}

	public PropertyChangeSupport getPropertyChange() {
		return propertyChange;
	}

	public void setPropertyChange(PropertyChangeSupport propertyChange) {
		this.propertyChange = propertyChange;
	}


	
	
	
}
